import java.util.concurrent.locks.ReentrantLock;

public class ThreadDemo20 {
    static class Counter{
        public int count=0;

        public ReentrantLock locker=new ReentrantLock();

        public void increse() {
            locker.lock();
            count++;
            locker.unlock();
        }
    }

    public static void main(String[] args) {
        Counter counter=new Counter();

        Thread t1=new Thread() {
            @Override
            public void run() {
                for (int i = 0; i <5_0000; i++) {
                    counter.increse();
                }
            }
        };

        Thread t2=new Thread() {
            @Override
            public void run() {
                for (int i = 0; i <5_0000; i++) {
                    counter.increse();
                }
            }
        };

        t1.start();
        t2.start();

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(counter.count);//线程不安全，每次的结果是不一样的，可以使用synchronized解决，或者 ReentrantLock解决
    }
}
